package com.daichi.yygh.cmn.service.impl;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.daichi.yygh.cmn.listener.DictListener;
import com.daichi.yygh.cmn.mapper.DictMapper;
import com.daichi.yygh.cmn.service.DictService;
import com.daichi.yygh.model.cmn.Dict;
import com.daichi.yygh.vo.cmn.DictEeVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class DictServiceImpl extends ServiceImpl<DictMapper, Dict> implements DictService {
    @Override
    public List<Dict> findChildrenData(Long id) {
        List<Dict> dictList = baseMapper.selectList(new QueryWrapper<Dict>().eq("parent_id", id));
        dictList.forEach(dict -> dict.setHasChildren(hasChildren(dict.getId())));
        return dictList;
    }

    @Override
    public void exportDictData(HttpServletResponse response) {

        // 设置下载信息
        String fileName = "dict";
        response.setContentType("application/vnd.ms-excel");
        response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
        response.setCharacterEncoding("UTF-8");

        // 查出信息并转换vo
        List<Dict> dictList = baseMapper.selectList(null);
        List<DictEeVo> voList = dictList.stream()
                .map(dict -> {
                    DictEeVo dictEeVo = new DictEeVo();
                    BeanUtils.copyProperties(dict, dictEeVo);
                    return dictEeVo;
                }).collect(Collectors.toList());

        // 调用EasyExcel输出
        try {
            EasyExcel.write(response.getOutputStream(), DictEeVo.class)
                    .sheet("dict")
                    .doWrite(voList);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void importDictData(MultipartFile file) {
        try {
            EasyExcel.read(file.getInputStream(), DictEeVo.class, new DictListener(baseMapper)).sheet().doRead();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public String getDictName(String dictCode, String value) {
        // 如果dictCode为空，直接根据value查询;否则用两个条件查询
        if (!StringUtils.hasLength(dictCode)) {
            System.out.println("=================================>"+value);
            Dict dict = baseMapper.selectOne(new QueryWrapper<Dict>().eq("value", value));
            return dict.getName();
        } else {
            Dict parentDict = baseMapper.selectOne(new QueryWrapper<Dict>().eq("dict_code", dictCode));
            Dict dict = baseMapper.selectOne(new QueryWrapper<Dict>()
                    .eq("parent_id", parentDict.getId())
                    .eq("value", value)
            );
            return dict.getName();
        }
    }

    @Override
    public List<Dict> findByDictCode(String dictCode) {
        Dict parentDict = baseMapper.selectOne(new QueryWrapper<Dict>().eq("dict_code", dictCode));

        List<Dict> dictList = baseMapper.selectList(new QueryWrapper<Dict>().eq("parent_id", parentDict.getId()));

        return dictList;
    }

    private boolean hasChildren(Long id) {
        Integer count = baseMapper.selectCount(new QueryWrapper<Dict>().eq("parent_id", id));
        return count > 0;
    }
}
